//
//  CZMenuViewController.m
//  QQ空间
//
//  Created by zhangping on 16/1/16.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZMenuViewController.h"
#import "CZTabBarController.h"
#import "CZMenuItemViewModel.h"
#import "CZMenuItem.h"
#import "CZMenuButton.h"
#import "CZDetailViewController.h"

const int MenuButtonHeight = 60;

const int ComposeViewLnadscapeHeight = 90;
const int ComposeViewPortraitHeight = 180;

const int IconTopMargin = 60;
const int IconLandscapeWH = 100;
const int IconPortraitWH = 44;

@interface CZMenuViewController ()

/// iPhone界面的容器视图
@property (weak, nonatomic) UIView *containerView;

/// 底部撰写视图(包含3个按钮)
@property (nonatomic, strong) UIStackView *composeView;

/// 菜单按钮
@property (nonatomic, strong) UIStackView *menuView;

/// 菜单项模型数组
@property (nonatomic, strong) CZMenuItemViewModel *menuItemVM;

/// 用户头像
@property (nonatomic, strong) UIButton *userIconButton;

/// 用户名称标签
@property (nonatomic, strong) UILabel *userNameLabel;

/// 选中的按钮
@property (nonatomic, weak) CZMenuButton *selectedButton;

@end

@implementation CZMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareUI];
}

#pragma mark - 准备UI
- (void)prepareUI {
    self.view.backgroundColor = CZCommonBGColor;
    
    [self prepareComposeView];
    [self prepareMenuView];
    [self prepareUserIconButton];
    [self prepareUserNameLabel];
    
    // 设置容器视图,需要放到菜单视图的最上面,当容器视图隐藏的时候就可以看到菜单相关视图
    [self setupPhoneContainerView];
    
    // 进入程序默认选中第一个 全部动态 按钮
    [self clickMenuButton:self.menuView.subviews[0]];
}

- (void)prepareUserNameLabel {
    [self.view addSubview:self.userNameLabel];
    
    self.userNameLabel.text = @"我要飞的更高";
    self.userNameLabel.textColor = [UIColor whiteColor];
    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.userIconButton.mas_bottom).offset(16);
    }];
}

- (void)prepareUserIconButton {
    [self.view addSubview:self.userIconButton];
    
    [self.userIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(IconTopMargin);
        make.size.mas_equalTo(CGSizeMake(IconLandscapeWH, IconLandscapeWH));
    }];
}

- (void)prepareComposeView {
    // 将stackView添加到self.view
    [self.view addSubview:self.composeView];
    
    // stackView添加约束
    [self.composeView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 左右底部和self.view对齐
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.equalTo(@(ComposeViewLnadscapeHeight));
    }];
    
    // 设置stackView布局方式
    self.composeView.distribution = UIStackViewDistributionFillEqually;
    
    for (CZMenuItem *item in self.menuItemVM.composeItems) {
        // stackView添加子控件
        CZMenuButton *button = [CZMenuButton buttonWithMenuItem:item];
        
        // 添加点击事件
        [button addTarget:self action:@selector(clickComposeButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.composeView addArrangedSubview:button];
    }
}

- (void)prepareMenuView {
    // 将stackView添加到self.view
    [self.view addSubview:self.menuView];
    
    // stackView添加约束,不指定布局方向的尺寸stackView会根据子控件的内容来自动约束
    [self.menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 左右和self.view对齐
        make.leading.right.equalTo(self.view);
        
        // 底部和composeView的顶部对齐
        make.bottom.equalTo(self.composeView.mas_top);
    }];
    
    // 设置布局方向
    self.menuView.axis = UILayoutConstraintAxisVertical;
    
    for (CZMenuItem *item in self.menuItemVM.menuItems) {
        // stackView添加子控件
        CZMenuButton *button = [CZMenuButton buttonWithMenuItem:item];
        
        // 添加点击事件
        [button addTarget:self action:@selector(clickMenuButton:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置按钮的高度
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(MenuButtonHeight));
        }];
        
        [self.menuView addArrangedSubview:button];
    }
}

#pragma mark - 按钮点击事件
/// 菜单按钮被点击
- (void)clickMenuButton:(CZMenuButton *)button {
    
    // 0.判断之前选中的按钮和点击的按钮是否是同一个
    if (self.selectedButton == button) {
        CZLog(@"已经选中了: %@", button.menuItem.title);
        return;
    }
    
    // 1.取消上次选中的按钮
    self.selectedButton.selected = NO;
    
    // 2.设置选中的按钮为选中状态
    button.selected = YES;
    
    // 3.设置当前点击按钮为选中按钮
    self.selectedButton = button;
    
    // 显示详情控制器
    CZDetailViewController *detailVC = self.splitViewController.viewControllers[1];
    [detailVC showControllerWithMenuItem:button.menuItem];
}

/// modal出对应的控制器
- (void)clickComposeButton:(CZMenuButton *)button {
    Class cls = NSClassFromString(button.menuItem.detailVCClassName);
    
    // 创建按钮对应的控制器
    UIViewController *vc = [[cls alloc] init];
    
    // 设置控制器标题
    vc.title = button.menuItem.title;
    
    // 包装导航栏
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    
    // modal出控制器
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - iPhone 容器视图相关代码
/// 设置容器视图
- (void)setupPhoneContainerView {
    // 1. 创建iPhone的控制器
    CZTabBarController *tabVC = [[CZTabBarController alloc] init];
    
    // 2. 将tabVC添加为当前控制器的子控制器
    [self addChildViewController:tabVC];
    
    // 3. 将tabVC的view设置为containerView
    self.containerView = tabVC.view;
    
    // 4. 将容器视图添加到self.view
    [self.view addSubview:self.containerView];
}

/**
 *  显示或隐藏iPhone界面的容器视图
 *  @param isShow YES:显示, NO:隐藏
 */
- (void)showContainerView:(BOOL) isShow {
    self.containerView.hidden = !isShow;
    
    // 设置状态栏样式,iPhone界面显示黑色状态栏
    [UIApplication sharedApplication].statusBarStyle = isShow ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
}

/**
 *  屏幕改变后,重新布局菜单和撰写按钮
 *  @param isLandscape 屏幕是否是横屏
 */
- (void)updateSubViews:(BOOL)isLandscape {
    
    // 设置composeView布局方向
    self.composeView.axis = isLandscape ? UILayoutConstraintAxisHorizontal: UILayoutConstraintAxisVertical;
    
    // 设置composeView高度
    [self.composeView mas_updateConstraints:^(MASConstraintMaker *make) {
        NSNumber *height = isLandscape ? @(ComposeViewLnadscapeHeight): @(ComposeViewPortraitHeight);
        make.height.equalTo(height);
    }];
    
    // 设置菜单按钮
    for (CZMenuButton *menuButton in self.menuView.subviews) {
        [menuButton setupMenuButton:isLandscape];
    }
    
    // 更新头像大小约束
    [self.userIconButton mas_updateConstraints:^(MASConstraintMaker *make) {
        CGSize size = isLandscape ? CGSizeMake(IconLandscapeWH, IconLandscapeWH) : CGSizeMake(IconPortraitWH, IconPortraitWH);
        make.size.mas_equalTo(size);
    }];
    
    // 竖屏不显示用户名
    self.userNameLabel.hidden = !isLandscape;
}

#pragma mark - 懒加载
/// 底部撰写视图(包含3个按钮)
- (UIStackView *)composeView {
    if (_composeView == nil) {
        _composeView = [[UIStackView alloc] init];
    }
    
    return _composeView;
}

/// 菜单按钮
- (UIStackView *)menuView {
    if (_menuView == nil) {
        _menuView = [[UIStackView alloc] init];
    }
    
    return _menuView;
}

- (CZMenuItemViewModel *)menuItemVM {
    if (_menuItemVM == nil) {
        _menuItemVM = [[CZMenuItemViewModel alloc] init];
    }
    
    return _menuItemVM;
}

- (UIButton *)userIconButton {
    if (_userIconButton == nil) {
        _userIconButton = [[UIButton alloc] init];
        [_userIconButton setImage:[UIImage imageNamed:@"default_person_lit"] forState:UIControlStateNormal];
    }
    
    return _userIconButton;
}

- (UILabel *)userNameLabel {
    if (_userNameLabel == nil) {
        _userNameLabel = [[UILabel alloc] init];
    }
    
    return _userNameLabel;
}
@end
